package com.briup.jdbc;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;

/**
 * 获取数据库的元数据
 */
public class GetDataBaseMetaData {
    public static void main(String[] args) {
        try {
            DataSource dataSource = DruidDataSourceFactory.createDataSource(DruidGetConnection.loadProperties());
            Connection connection = dataSource.getConnection();
            //获取元数据对象
            DatabaseMetaData metaData = connection.getMetaData();
            //获取数据库名字
            System.out.println(metaData.getDatabaseProductName());
            //获取数据库名字以及版本
            System.out.println(metaData.getDatabaseProductVersion());
//            获取数据库大版本
            System.out.println(metaData.getDatabaseMajorVersion());
//            获取数据库小版本
            System.out.println(metaData.getDatabaseMinorVersion());
            System.out.println(metaData.getSystemFunctions());
            //获取表信息

            ResultSet tables = metaData.getTables(null, "BTTC2", "S_EMP", null);

            //结果集元数据
            ResultSetMetaData metaData1 = tables.getMetaData();
            //结果集 列计数
            int count = metaData1.getColumnCount();

            //处理结果
            while (tables.next())
            {
                for (int i = 1; i <=count ; i++) {
                    String columnName = metaData1.getColumnName(i);

                    int columnType = metaData1.getColumnType(i);
                    String value = tables.getString(i);
                    System.out.println(columnName+":"+value);

                }

            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
